<?php

/**
 * player actions.
 *
 * @package    s-test
 * @subpackage player
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class playerActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->sf_guard_users = Doctrine::getTable('sfGuardUser')
      ->createQuery('a')
      ->execute();
  }

  public function executeActivate(sfWebRequest $request){
      $this->sf_guard_user = Doctrine::getTable('sfGuardUser')->find(array($request->getParameter('id')));

      $this->forward404Unless($this->sf_guard_user, "Le compte joueur n'existe pas...");
      $this->forward404Unless($this->sf_guard_user->isActivationToken($request->getParameter("activation_token")), "Vous n'avez pas le bon jeton d'activation...");

      $this->sf_guard_user->setIsActive(true);
      $this->sf_guard_user->save();

      $this->getUser()->setFlash("Votre compte est désormais activé ! Amusez vous =)");
      $this->redirect("@homepage");
  }
  public function executeMenu(sfWebRequest $request){
     if($request->isXmlHttpRequest()){
      if(!$this->getUser()->isAuthenticated())
              return $this->renderText("Non connecté.");
      else{
          return $this->renderPartial('player/player-menu', array('sf_guard_user'=> $this->getUser()->getGuardUser()));
      }
     }
      
  }
    //Fonction appelée par l'updater Ajax afin de notifier les nouveaux éléments au joueur
    public function executeNotification(sfWebRequest $request){
        if($request->isXmlHttpRequest()){
            /*A taffer*/
        }
        
    }
    public function executeDeleteNotifications(sfWebRequest $request){
        $this->not = Doctrine::getTable('KTDNotification')->find(array($request->getParameter('id')));
        $this->forward404Unless($this->not);
        if($this->not->getPlayerId() == $this->getUser()->getGuardUser()->getId())
                $this->not->delete();
        else
            $this->forward404 ("Vous ne pouvez pas supprimer cette notifications");

        $this->redirect($this->generateUrl("show_notifications"));
    }
    public function executeShowNotifications(sfWebRequest $request){
        $this->player = $this->getUser()->getGuardUser();
    }
    public function executeAutocomplete(sfWebRequest $request){
        $term = $request->getParameter('term', '');
        $players = sfGuardUser::doAutocompleteQuery($term);
        $rv = NULL;
        foreach($players as $pl){
            if($rv != NULL) $rv .= ', ';
            $rv .= '{ title: "'.htmlspecialchars($pl->getPseudo(), ENT_QUOTES, 'UTF-8').'",
                      id: '.$pl->getId().'
                    }';
        }
        return $this->renderText("[$rv]");
    }
    public function executeAutocompletePseudo(sfWebRequest $request){
        $term = $request->getParameter('term', '');
        $players = sfGuardUser::doAutocompleteQuery($term);
        $rv = NULL;
        foreach($players as $pl){
            if($rv != NULL) $rv .= ', ';
            $rv .= '{ title: "'.htmlspecialchars($pl->getPseudo(), ENT_QUOTES, 'UTF-8').'",
                      id: '.$pl->getId().'
                    }';
        }
        return $this->renderText("[$rv]");
    }
    public function executeSwitchcharacter(sfWebRequest $request){
      //Si on provient d'une requête Ajax via le menu joueur.
      if($request->isXmlHttpRequest() || $request->getParameter('character_id') != NULL){
      $this->actual_character = Doctrine::getTable('KTDCharacter')->find(array($request->getParameter('character_id')));
      $this->forward404Unless($this->actual_character, 'Ce personnage n\'existe pas...');
          foreach($this->getUser()->getGuardUser()->getKTDCharacters() as $ktd_character){
            if($ktd_character == $this->actual_character){
                $this->getUser()->getGuardUser()->setActualCharacter($this->actual_character);
                $this->getUser()->getGuardUser()->save();
                if($request->isXmlHttpRequest())
                return $this->renderPartial ('player/actual-character-link', array('ktd_character'=>$this->actual_character));
                $this->redirect($this->generateUrl("show_player", $this->getUser()->getGuardUser()));

            }
          }
      }
      else{
            $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT), "La requête n'est pas du type attendue. (POST)");
            $this->sf_guard_user = Doctrine::getTable('sfGuardUser')->find(array($request->getParameter('id')));
            $this->forward404Unless($this->sf_guard_user, sprintf('Le joueur suivant n\'existe pas (%s).', $request->getParameter('id')));
            $this->forward404Unless($this->getUser()->getGuardUser()->hasRelPermission('edit_profil', $this->sf_guard_user), 'Vous ne pouvez pas éditer ce profil !');

             $this->form = new KTDSwitchCharacterForm($this->sf_guard_user);
             $this->processForm($request, $this->form);
             $this->setTemplate("showcharacters");
      }
  }
  public function executeShowcharacters(sfWebRequest $request){
      $this->sf_guard_user = Doctrine::getTable('sfGuardUser')->find(array($request->getParameter('id')));
      $this->forward404Unless($this->sf_guard_user);
      $this->forward404Unless($this->sf_guard_user->hasCharacters(), 'Ce joueur n\'a pas de personnage !');

      $this->form = new KTDSwitchCharacterForm($this->sf_guard_user);
      
  }
  public function executeShow(sfWebRequest $request)
  {
    $this->sf_guard_user = Doctrine::getTable('sfGuardUser')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->sf_guard_user);
    $this->switch_char_form = new KTDSwitchCharacterForm($this->sf_guard_user);
  }
  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($this->sf_guard_user = Doctrine::getTable('sfGuardUser')->find(array($request->getParameter('id'))), sprintf('Object sf_guard_user does not exist (%s).', $request->getParameter('id')));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasRelPermission('edit_profil', $this->sf_guard_user), 'Vous ne pouvez pas éditer ce profil !');

    $this->form = new KTDPlayerForm($this->sf_guard_user);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($sf_guard_user = Doctrine::getTable('sfGuardUser')->find(array($request->getParameter('id'))), sprintf('Object sf_guard_user does not exist (%s).', $request->getParameter('id')));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasRelPermission('edit_profil', $sf_guard_user), 'Vous ne pouvez pas éditer ce profil !');

    $this->form = new KTDPlayerForm($sf_guard_user);
    $this->processForm($request, $this->form);
    $this->setTemplate('edit');
  }
  public function executeWhoIsOnline(sfWebRequest $request){
      $this->online_players = sfGuardUser::getOnlineUsers();
      
  }
  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($sf_guard_user = Doctrine::getTable('sfGuardUser')->find(array($request->getParameter('id'))), sprintf('Object sf_guard_user does not exist (%s).', $request->getParameter('id')));
    $sf_guard_user->delete();

    $this->redirect('player/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    
    if ($form->isValid())
    {
      $sf_guard_user = $form->save();
      if($form->getName() == "ktd_player")
        $this->redirect('show_player', $sf_guard_user);
      else if($form->getName() == "ktd_switch_character")
        $this->redirect("show_player", $sf_guard_user);
      else
        $this->redirect('show_player', $sf_guard_user);
    }
    
  }
}
